package com.hzqy.service.industry.medical;

import com.hzqy.persistence.dao.industry.SdGoodOrderDao;
import com.hzqy.persistence.dao.industry.SdUserDao;
import com.hzqy.web.vo.industry.medical.SdGoodOrderVo;
import com.hzqy.web.vo.industry.medical.SdUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SdUserService {
    
    protected final Logger LOG = LoggerFactory.getLogger(getClass());
    @Resource
    private SdUserDao sdUserDao;
    @Resource
    private SdGoodOrderDao sdGoodOrderDao;
    
    public Map<String, Object> insertUserInfo(SdUserVo sdUserVo) {
        LOG.debug("进入insertUserInfo:"+sdUserVo+" 26行");
        Map<String,Object> resultMap=new HashMap<>();
        int i=sdUserDao.insertUserInfo(sdUserVo);
        if (i<=0){
            resultMap.put("code","201");
            resultMap.put("message","添加用户信息失败");
            LOG.debug("insertUserInfo:"+resultMap.get("message")+" 32行");
            return resultMap;
        }else{
            resultMap.put("code","200");
            resultMap.put("message","添加用户信息成功");
        }
        LOG.debug("insertUserInfo:"+resultMap.get("message")+" 38行");
        return resultMap;
    }

    public Map<String, Object> updateUserInfo(SdUserVo sdUserVo) {
        LOG.debug("进入updateUserInfo:"+sdUserVo+" 43行");
        Map<String,Object> resultMap=new HashMap<>();
        int i=sdUserDao.updateUserInfo(sdUserVo);
        if (i<=0){
            resultMap.put("success",false);
            resultMap.put("message","修改用户信息失败");
            LOG.debug("updateUserInfo:"+resultMap.get("message")+" 49行");
            return resultMap;
        }else{
            resultMap.put("success",true);
            resultMap.put("message","修改用户信息成功");
        }
        LOG.debug("updateUserInfo:"+resultMap.get("message")+" 55行");
        return resultMap;
    }

    public SdUserVo selectUserById(SdUserVo sdUserVo) {
        LOG.debug("进入selectUserById:"+sdUserVo+" 60行");
        return sdUserDao.selectUserById(sdUserVo);
    }

    public Map<String, Object> insertUserInfot(SdUserVo sdUserVo) {
        LOG.debug("进入insertUserInfot:"+sdUserVo+" 65行");
        Map<String, Object> resultMap =new HashMap<>();
        List<SdUserVo> list=sdUserDao.selectUserTrue(sdUserVo);
        SdGoodOrderVo sdGoodOrderVo=new SdGoodOrderVo();
        if (list==null||list.size()==0){
            int i=sdUserDao.insertUserInfot(sdUserVo);
            if (i<=0){
                resultMap.put("success",false);
                resultMap.put("message","添加用户失败");
                LOG.debug("insertUserInfot:"+resultMap.get("message")+" 74行");
                return resultMap;
            }
            resultMap.put("uid",sdUserVo.getF_sd_uid());
            sdGoodOrderVo.setF_sd_money(0);
            sdGoodOrderVo.setF_sd_uid(sdUserVo.getF_sd_uid());
            sdGoodOrderVo.setF_sd_village(sdUserVo.getF_sd_village());
            sdGoodOrderVo.setF_sd_buyTime(sdUserVo.getF_sd_buyTime());
            sdGoodOrderVo.setF_sd_sellByDou(sdUserVo.getF_sd_sellByDou());
        }else{
            resultMap.put("uid",list.get(0).getF_sd_uid());
            sdGoodOrderVo.setF_sd_money(0);
            sdGoodOrderVo.setF_sd_uid(list.get(0).getF_sd_uid());
            sdGoodOrderVo.setF_sd_village(list.get(0).getF_sd_village());
            sdGoodOrderVo.setF_sd_buyTime(list.get(0).getF_sd_buyTime());
            sdGoodOrderVo.setF_sd_sellByDou(list.get(0).getF_sd_sellByDou());
        }
        int i1 = sdGoodOrderDao.addDictOrderTotal(sdGoodOrderVo);
        if (i1>0){
            resultMap.put("oid",sdGoodOrderVo.getF_sd_oid());
            resultMap.put("success",true);
            resultMap.put("message","添加用户成功");
        }else{
            resultMap.put("success",false);
            resultMap.put("message","添加用户失败");
        }
        LOG.debug("insertUserInfot:"+resultMap.get("message")+" 95行");
        return resultMap;
    }
    
    public Map<String, Object> insertUsersInfot(SdUserVo sdUserVo) {
        LOG.debug("进入insertUsersInfot:"+sdUserVo+" 103行");
        Map<String, Object> resultMap =new HashMap<>();
        List<SdUserVo> list=sdUserDao.selectUserTrue(sdUserVo);
        SdGoodOrderVo sdGoodOrderVo=new SdGoodOrderVo();
        if (list==null||list.size()==0){
            int i=sdUserDao.insertUserInfot(sdUserVo);
            if (i<=0){
                resultMap.put("success",false);
                resultMap.put("message","添加用户失败");
                LOG.debug("insertUserInfot:"+resultMap.get("message")+" 112行");
                return resultMap;
            }
            resultMap.put("uid",sdUserVo.getF_sd_uid());
            sdGoodOrderVo.setF_sd_money(0);
            sdGoodOrderVo.setF_sd_uid(sdUserVo.getF_sd_uid());
            sdGoodOrderVo.setF_sd_village(sdUserVo.getF_sd_village());
            sdGoodOrderVo.setF_sd_buyTime(sdUserVo.getF_sd_buyTime());
            sdGoodOrderVo.setF_sd_orderid(sdUserVo.getF_sd_orderid());
            sdGoodOrderVo.setF_sd_sellByDou(sdUserVo.getF_sd_sellByDou());
            int i1 = sdGoodOrderDao.addDictOrderTotal(sdGoodOrderVo);
            if (i1>0){
                resultMap.put("oid",sdGoodOrderVo.getF_sd_oid());
                resultMap.put("success",true);
                resultMap.put("message","添加用户成功");
            }else{
                resultMap.put("success",false);
                resultMap.put("message","添加用户失败");
            }
        }else{
            resultMap.put("uid",list.get(0).getF_sd_uid());
            List<SdGoodOrderVo> list1=sdUserDao.selectTotalOrder(sdUserVo);
            if (list1==null||list1.size()==0){
                resultMap.put("success",false);
                resultMap.put("message","添加用户失败");
            }else{
                System.out.println(list1.get(0).getF_sd_oid());
                resultMap.put("oid",list1.get(0).getF_sd_oid());
                resultMap.put("success",true);
                resultMap.put("message","添加用户成功");
            }
        }
        LOG.debug("insertUserInfot:"+resultMap.get("message")+" 137行");
        return resultMap;
    }
}
